package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.entity.CustomerRuleEntity;
import com.xbongbong.saas.domain.entity.CustomerUserEntity;
import com.xbongbong.saas.domain.entity.RuleCustomerEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.CustomerUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 前负责人规则功能类
 * @author : chy
 * @date 创建时间：2018-5-16 下午6:51:08
 * @version v3.22
 */
@Component
public class CheckRulePrePrincipal implements ICheckRule {

	@Resource
	private CustomerUserModel customerUserModel;
	@Resource
	private CustomerModel customerModel;

	@Override
	public boolean canGainCustomer(CustomerRuleEntity rule, String corpid,
								   UserEntity userEntity, Long customerId, int dataIdSize, JSONObject data) throws XbbException {
		//没有规则，不限制客户获取
		if(rule == null || Objects.equals(rule.getStatus(), 0)){
			return true;
		}

		//判断是否公海客户
		CustomerEntityExt customer = customerModel.getByKey(customerId, corpid);

		Integer isPublic = customer.getData().getInteger(CustomerManagementEnum.IS_PUBLIC.getAttr());

		if(Objects.equals(isPublic, 0)){
			//非公海客户不限制
			return true;
		}

		//判断是否前负责人
		List<CustomerUserEntity> customerUserList = getCustomerUserList(corpid, userEntity.getUserId(), customerId);
		//为空不是前负责人
		if(customerUserList==null || customerUserList.isEmpty()){
			return true;
		}

		/**
		 * rule.getRuleType() 必然是 CustomerRuleEnum.PRE_PRINCIPAL_RULE.getCode()
		 */
		IOperateRuleValue operateRuleValue = OperateRuleValueFactory.operateRuleValueFactory(rule.getRuleType());
		JSONArray ruleValueArray = (JSONArray) operateRuleValue.toObject(rule);

		Integer minValue = CheckRuleUtil.getMinValue(ruleValueArray, userEntity);

		CustomerUserEntity customerUser = customerUserList.get(0);
		// 退回时间
		Long backTime = customerUser.getBackTime();
		Long now = DateTimeUtil.getInt();

		long minSecond = minValue * 86400L;

		if (now - backTime > minSecond) {
			return true;
		}

		return false;
	}

	public List<CustomerUserEntity> getCustomerUserList(String corpid, String userId, Long customerId){

		//关联表结构没有改变  使用以前的代码逻辑
		Map<String, Object> param = new HashMap<>(6);
		param.put("customerId", customerId);
		param.put("isMain", 1);
		param.put("corpid", corpid);
		param.put("userId", userId);
		param.put("orderByStr", "back_time desc");
		param.put("delIn", Arrays.asList(1,-1));
		param.put("pageSize", 1);

		return customerUserModel.findEntitys(param);
	}

	@Override
	public List<RuleCustomerEntity> getBackCustomerList(UserEntity userEntity,
														CustomerRuleEntity rule, List<RuleCustomerEntity> customerList,
														Integer advanceTime) throws XbbException {
		return new ArrayList<>();
	}

	@Override
	public List<RuleCustomerEntity> getBackCustomerList(UserEntity userEntity,
			CustomerRuleEntity rule, List<RuleCustomerEntity> customerList)
			throws XbbException {
		return new ArrayList<>();
	}

	@Override
	public JSONObject customerTypeChage(CustomerRuleEntity rule, List<ItemPoJo> typeItemPoJos) throws XbbException {
		return null;
	}

}
